﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

using AgencyEntityLib.BaseEntity;

using Ecopsy.Tools;
using OutdoorLib.BaseEntity.WS;
using Grishko.DataTier;
using OutdoorLib.BaseEntity.Agency;
using OutdoorLib.BaseEntity.Face;
using OutdoorLib.BaseEntity.Dictionary;
using Grishko.UI;
using Grishko.DataTier.SqlSentence;
using System.Collections.Generic;
using OutdoorLib.BaseEntity.Filters;

public partial class Frame_Managment : System.Web.UI.Page 
{
    


    protected void Page_Load(object sender, EventArgs e)
    {
        tvConstractionType.BuildNode +=new TreeControl._BuildNode (tvConstractionType_BuildNode);
        tvDepartmentTree.BuildNode += new TreeControl._BuildNode(tvDepartmentTree_BuildNode );
        tvPriceList.BuildNode += new TreeControl._BuildNode(tvPriceList_BuildNode);
        tvDiscounts.BuildNode += new TreeControl._BuildNode(tvDiscounts_BuildNode);
        tvGroups.BuildNode += new TreeControl._BuildNode(tvGroups_BuildNode);        

    
        if (!IsPostBack)
        {
            BindTree();
        }
    }


    protected Control tvConstractionType_BuildNode(Grishko .UI .TreeControl  tree, TreeEntitySingle nd)
    {    
            LinkButton btn = new LinkButton();
            btn.CssClass = tree.CssNode;
            btn.ID = nd.ID;
            btn.Text = (nd as DictionaryItem).Value.ToString () ;
            string sConstrType="";
            if (!(nd is TreeDictionaryItem))
                sConstrType = GlobalConstants .qParamSplitter  + ConstractionListFilter.qConstractionType + "=" + nd.ID;

            string[] param = { "ConstractionListFilter", sConstrType };
            string sUrl = string.Format("ListForm_Default.aspx?ListTagName={0}{1}", param);
            
            string[] param1 = { sUrl, tree.CssNode, tree.CssSelectedNode,nd.ID };
            btn.Attributes.Add("onClick", String.Format("onSelectBranch('{0}','{1}','{2}','{3}');", param1));
            return btn;
    }

    protected Control tvDepartmentTree_BuildNode(TreeControl tree, TreeEntitySingle nd)
    {
        LinkButton btn = new LinkButton();
        btn.ID = nd.ID;
        btn.CssClass = tree.CssNode;
        btn.Text = (nd as DictionaryItem).Value.ToString();
        
        string sDepatmentId = "";
        sDepatmentId = GlobalConstants.qParamSplitter + EmployeeListFilter.qDepartmentID + "=" + nd.ID;

        string[] param = { "EmployeeListFilter", sDepatmentId };
        string sUrl = String.Format("ListForm_Default.aspx?ListTagName={0}{1}", param);
        string[] param1 = { sUrl, tree.CssNode, tree.CssSelectedNode, nd.ID };
        btn.Attributes.Add("onClick", String.Format("onSelectBranch('{0}','{1}','{2}','{3}');", param1));
        
        return btn;
    }


    protected Control tvPriceList_BuildNode(Grishko.UI.TreeControl tree, TreeEntitySingle nd)
    {
        LinkButton btn = new LinkButton();
        btn.CssClass = tree.CssNode;
        btn.ID = nd.ID;
        btn.Text = (nd as DictionaryItem).Value.ToString();
        string sConstrType = "";
        if (!(nd is TreeDictionaryItem))
            sConstrType = GlobalConstants.qParamSplitter + ConstractionListFilter.qConstractionType + "=" + nd.ID;

        string[] param = { "SideListPriceFilter" };
        string sUrl = String.Format("PriceListEditor.aspx?ListTagName={0}", param);

        string[] param1 = { sUrl, tree.CssNode, tree.CssSelectedNode, nd.ID };
        btn.Attributes.Add("onClick", String.Format("onSelectBranch('{0}','{1}','{2}','{3}');", param1));
        return btn;
    }

    protected Control tvDiscounts_BuildNode(Grishko.UI.TreeControl tree, TreeEntitySingle nd)
    {
         
        LinkButton btn = new LinkButton();
        btn.CssClass = tree.CssNode;
        btn.ID = nd.ID;
        btn.Text = (nd as DictionaryItem).Value.ToString();
        string sConstrType = "";
        if (!(nd is TreeDictionaryItem))
            sConstrType = GlobalConstants.qParamSplitter + ConstractionListFilter.qConstractionType + "=" + nd.ID;


        string sUrl = "DiscountList.aspx";

        string[] param1 = { sUrl, tree.CssNode, tree.CssSelectedNode, nd.ID };
        btn.Attributes.Add("onClick", String.Format("onSelectBranch('{0}','{1}','{2}','{3}');", param1));
        return btn;
    }

    protected Control tvGroups_BuildNode(Grishko.UI.TreeControl tree, TreeEntitySingle nd)
    {
        string sID = (nd as DictionaryItem).Value.ToString();
        LinkButton btn = new LinkButton();
        btn.CssClass = tree.CssNode;
        btn.ID = sID;
        btn.Text = (nd as DictionaryItem).ParamID.ToString();
        //string sConstrType = "";
        //if (!(nd is TreeDictionaryItem))
        //    sConstrType = GlobalConstants.qParamSplitter + ConstractionListFilter.qConstractionType + "=" + nd.ID;

        
        string sUrl = "GroupList.aspx?" + GlobalConstants.qTagName + "=" + (nd as DictionaryItem).Value.ToString();
        sUrl=sUrl+"&GroupType="+sID ;

        string[] param1 = { sUrl, tree.CssNode, tree.CssSelectedNode, sID };
        btn.Attributes.Add("onClick", String.Format("onSelectBranch('{0}','{1}','{2}','{3}');", param1));
        return btn;
    }

    protected void OnRefresh(object sender, ImageClickEventArgs arg)
    {
        BindTree();
    }

    private void BindTree()
    {
        tvConstractionType.DataSource = ConstractionType;
        tvConstractionType.DataBind();

        tvDepartmentTree.DataSource = AppEnvironment.CurrentAgency.Department;
        tvDepartmentTree.DataBind();

        tvDiscounts .DataSource =Discounts;
        tvDiscounts .DataBind ();

        tvGroups.DataSource = Groups ;
        tvGroups.DataBind();

        tvPriceList.DataSource = PriceList;
        tvPriceList.DataBind();



    }
    
    /// <summary>
    /// Справочник конструкций
    /// </summary>
    protected TreeDictionaryItem ConstractionType
    {
        get { return AppEnvironment.DictionarySource.GetTreeDictionaryItemWithTypeID(TreeDictionaryItem.TreeDictionary_ConstractionType); }
    }

    private void AddBranchCollection(EntityList list, TreeDictionaryItem Parent)
    {
        List<OutdoorLib.BaseEntity.Dictionary.TreeDictionaryItem.BranchItem> result = new List<TreeDictionaryItem.BranchItem>();
        foreach (DictionaryItem i in list)
        {
            Parent.ChildNodes.Add(new TreeDictionaryItem.BranchItem() { ParamID = i.ID, Value = i.Value });
        }
    }

    protected TreeDictionaryItem Discounts
    {
        get 
        { 
            TreeDictionaryItem result= new TreeDictionaryItem (){ParamID ="DiscountTree" ,Name="Скидки"};
            //AddBranchCollection(AppEnvironment.CurrentAgency.Discounts, result);
            return result ;
        }
    }

    protected TreeDictionaryItem PriceList
    {
        get
        {
            TreeDictionaryItem result = new TreeDictionaryItem() { ParamID = "PriceListTree", Name = "Прайс лист" };
            //AddBranchCollection(AppEnvironment.CurrentAgency.Discounts, result);
            return result;
        }
    }

    protected TreeDictionaryItem Groups
    {
        get 
        {
            TreeDictionaryItem result = new TreeDictionaryItem() { ParamID = "Группы", Name = "GroupTree" };
            foreach (DictionaryItem item in TGroupList.GroupTypes)
            {
                result.ChildNodes.Add(new TreeDictionaryItem.BranchItem() { ParamID = item.ParamID , Value = item.Value });
            }
            return result;
        }
    }


}
